const express = require('express')
const router = express.Router()
// 上传文件
const multer = require('multer')
// 设置参数 formdata
const db=require('../database/index')
const upload = multer({ dest: 'uploads/' })
router.post('/article/add', upload.single('cover_img'), (req, res) => {
    // console.log(req.file); //文件信息
    // console.log('-----------');
    // console.log(req.body);  //普通字段
    // res.send('发布文章成功')
    let {title,cate_id,content,state}=req.body
    let cover_img='/uploads/'+req.file.filename
    let author_id=req.user.id
    let pub_date='1111-11-11 11:11:11'
    let is_delete=0
    // 生成sql
    let sql = `insert into article set title="${title}",cate_id="${cate_id}",content="${content}",state="${state}",cover_img="${cover_img}",author_id="${author_id}",pub_date="${pub_date}",is_delete=${is_delete}`
    db(sql,(err,results)=>{
        if(err) throw err
        if (results.affectedRows !== 1 ) {
            return res.send({status:0 , message:'添加失败！'})
        }
        res.send({status:1,message:'添加成功！'})
    })
})
module.exports = router